package com.pda.yuanjing.pages.syzs;

import android.content.IntentFilter;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatButton;

import com.pda.yuanjing.R;
import com.pda.yuanjing.beanout.CodeQueryBeanOut;
import com.pda.yuanjing.beanout.CodeSYZSBeanOut;
import com.pda.yuanjing.boardcast.CustomReceiver;
import com.pda.yuanjing.boardcast.ScannerInterface;
import com.public_module.basemvp.activity.BaseActivity;
import com.public_module.tools.SharedPreferencesManger;
import com.public_module.tools.StringProcess;
import com.public_module.tools.VoiceUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SYZSActivity extends BaseActivity<PageModel, PageView, PagePresent> implements PageView {


    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tvr)
    TextView tvr;
    @BindView(R.id.rltitle)
    RelativeLayout rltitle;
    @BindView(R.id.oet)
    EditText oet;
    @BindView(R.id.bt)
    Button bt;
    @BindView(R.id.et1)
    EditText et1;
    @BindView(R.id.ll1)
    LinearLayout ll1;
    @BindView(R.id.et2)
    EditText et2;
    @BindView(R.id.ll2)
    LinearLayout ll2;
    @BindView(R.id.et3)
    EditText et3;
    @BindView(R.id.ll3)
    LinearLayout ll3;
    @BindView(R.id.et4)
    EditText et4;
    @BindView(R.id.ll4)
    LinearLayout ll4;
    @BindView(R.id.et5)
    EditText et5;
    @BindView(R.id.ll5)
    LinearLayout ll5;
    @BindView(R.id.et6)
    EditText et6;
    @BindView(R.id.ll6)
    LinearLayout ll6;
    @BindView(R.id.et7)
    EditText et7;
    @BindView(R.id.ll7)
    LinearLayout ll7;
    @BindView(R.id.et8)
    EditText et8;
    @BindView(R.id.ll8)
    LinearLayout ll8;
    @BindView(R.id.et9)
    EditText et9;
    @BindView(R.id.ll9)
    LinearLayout ll9;
    @BindView(R.id.et10)
    EditText et10;
    @BindView(R.id.ll10)
    LinearLayout ll10;
    @BindView(R.id.et11)
    EditText et11;
    @BindView(R.id.ll11)
    LinearLayout ll11;
    @BindView(R.id.et12)
    EditText et12;
    @BindView(R.id.ll12)
    LinearLayout ll12;
    @BindView(R.id.et13)
    EditText et13;
    @BindView(R.id.ll13)
    LinearLayout ll13;
    @BindView(R.id.et14)
    EditText et14;
    @BindView(R.id.ll14)
    LinearLayout ll14;
    @BindView(R.id.et15)
    EditText et15;
    @BindView(R.id.ll15)
    LinearLayout ll15;
    @BindView(R.id.et16)
    EditText et16;
    @BindView(R.id.ll16)
    LinearLayout ll16;
    @BindView(R.id.et17)
    EditText et17;
    @BindView(R.id.ll17)
    LinearLayout ll17;
    @BindView(R.id.et18)
    EditText et18;
    @BindView(R.id.ll18)
    LinearLayout ll18;
    @BindView(R.id.et19)
    EditText et19;
    @BindView(R.id.ll19)
    LinearLayout ll19;
    @BindView(R.id.et20)
    EditText et20;
    @BindView(R.id.ll20)
    LinearLayout ll20;
    @BindView(R.id.btt)
    AppCompatButton btt;

    private String TAG = "SYZSActivity";
    ScannerInterface scanner;
    CustomReceiver customReceiver = null;
    VoiceUtils voiceUtils;
    CodeSYZSBeanOut codeSYZSBeanOutm = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_syzs);
        ButterKnife.bind(this);
        tvTitle.setText("实验指示 " + SharedPreferencesManger.getRealName());
        scanner = new ScannerInterface(this);
        scanner.setOutputMode(1);
        customReceiver = new CustomReceiver(
                new CustomReceiver.PListener1() {
                    @Override
                    public void setData(CustomReceiver.Dataa dataa) {
                    }
                },
                new CustomReceiver.PListener2() {
                    @Override
                    public void setText(String string) {
                        String ss = StringProcess.codeMat(string);
                        presenter.query(ss);
                    }
                });
        IntentFilter filter = new IntentFilter();
        filter.addAction(ScannerInterface.RES_ACTION);
        registerReceiver(customReceiver, filter);
        voiceUtils = new VoiceUtils(this);
        //oet.setText("49647842576879");
        et1.setInputType(InputType.TYPE_NULL);
        et2.setInputType(InputType.TYPE_NULL);
        et3.setInputType(InputType.TYPE_NULL);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (customReceiver != null) {
            unregisterReceiver(customReceiver);
        }
        if (scanner != null) {
            scanner.setOutputMode(0);
        }
    }

    @Override
    public PageModel createModel() {
        return new PageModelImpl(this);
    }

    @Override
    public PageView createView() {
        return this;
    }

    @Override
    public PagePresent createPresenter() {
        return new PagePresent();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void tip(String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showMsg(str);
            }
        });
    }

    @Override
    public void tipS(CodeSYZSBeanOut codeSYZSBeanOut) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                codeSYZSBeanOutm = codeSYZSBeanOut;
                et1.setText(codeSYZSBeanOut.getData().getName());
                et2.setText(codeSYZSBeanOut.getData().getSpecs());
                et3.setText(codeSYZSBeanOut.getData().getCode());
                et4.setText(codeSYZSBeanOut.getData().getA0004());
                et5.setText(codeSYZSBeanOut.getData().getA0005());
                et6.setText(codeSYZSBeanOut.getData().getA0006());
                et7.setText(codeSYZSBeanOut.getData().getA0007());
                et8.setText(codeSYZSBeanOut.getData().getA0008());
                et9.setText(codeSYZSBeanOut.getData().getA0009());
                et10.setText(codeSYZSBeanOut.getData().getA00010());
                et11.setText(codeSYZSBeanOut.getData().getA00011());
                et12.setText(codeSYZSBeanOut.getData().getA00012());
                et13.setText(codeSYZSBeanOut.getData().getA00013());
                et14.setText(codeSYZSBeanOut.getData().getA00014());
                et15.setText(codeSYZSBeanOut.getData().getA00015());
                et16.setText(codeSYZSBeanOut.getData().getA00016());
                et17.setText(codeSYZSBeanOut.getData().getA00017());
                et18.setText(codeSYZSBeanOut.getData().getA00018());
                et19.setText(codeSYZSBeanOut.getData().getA00019());
                et20.setText(codeSYZSBeanOut.getData().getA00020());
            }
        });
    }

    @OnClick({R.id.iv_title_left, R.id.bt,R.id.btt})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.bt:
                presenter.query(oet.getText().toString());
                break;
            case R.id.btt:
                if(codeSYZSBeanOutm==null){
                    showMsg("当前没有实验数据");
                    return;
                }
                codeSYZSBeanOutm.getData().setName(et1.getText().toString());
                codeSYZSBeanOutm.getData().setSpecs(et2.getText().toString());
                codeSYZSBeanOutm.getData().setCode(et3.getText().toString());
                codeSYZSBeanOutm.getData().setA0004(et4.getText().toString());
                codeSYZSBeanOutm.getData().setA0005(et5.getText().toString());
                codeSYZSBeanOutm.getData().setA0006(et6.getText().toString());
                codeSYZSBeanOutm.getData().setA0007(et7.getText().toString());
                codeSYZSBeanOutm.getData().setA0008(et8.getText().toString());
                codeSYZSBeanOutm.getData().setA0009(et9.getText().toString());
                codeSYZSBeanOutm.getData().setA00010(et10.getText().toString());
                codeSYZSBeanOutm.getData().setA00011(et11.getText().toString());
                codeSYZSBeanOutm.getData().setA00012(et12.getText().toString());
                codeSYZSBeanOutm.getData().setA00013(et13.getText().toString());
                codeSYZSBeanOutm.getData().setA00014(et14.getText().toString());
                codeSYZSBeanOutm.getData().setA00015(et15.getText().toString());
                codeSYZSBeanOutm.getData().setA00016(et16.getText().toString());
                codeSYZSBeanOutm.getData().setA00017(et17.getText().toString());
                codeSYZSBeanOutm.getData().setA00018(et18.getText().toString());
                codeSYZSBeanOutm.getData().setA00019(et19.getText().toString());
                codeSYZSBeanOutm.getData().setA00020(et20.getText().toString());
                presenter.mod(codeSYZSBeanOutm);
                break;
        }
    }
}